﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity.Core;
using System.Data.SqlClient;

namespace CreateEntity
{
    public partial class AddSQL : Form
    {
        DataBaseTree dbTree;
        public AddSQL(DataBaseTree dbTree)
        {
            this.dbTree = dbTree;
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddSQL_Load(object sender, EventArgs e)
        {
            this.cbAuthentication.SelectedIndex = 0;
        }

        private void cbAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAuthentication.SelectedIndex == 0)
            {
                //SQL身份验证
                this.txbPwd.Enabled = this.txbUserName.Enabled = true;
            }
            else
            {
                this.txbPwd.Enabled = this.txbUserName.Enabled = false;
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            ConnectionModel model = new ConnectionModel();
            model.ConnectionString = GetConnectionString();
            model.DataBaseName = cbDataBase.Text;
            model.Provider = "System.Data.SqlClient";

            Connections con = new Connections();
            if (con.CheckConnection(model))
            {
                MessageBox.Show("连接成功！", "提示",MessageBoxButtons.OK,MessageBoxIcon.Question);
                string sql = "select * from sysdatabases";
                using (IDbConnection conn = new SqlConnection(model.ConnectionString))
                {
                    conn.Open();
                    IDbCommand com = conn.CreateCommand();
                    com.CommandText = sql;
                    IDataReader reader = com.ExecuteReader(CommandBehavior.CloseConnection);
                    while (reader.Read())
                    {
                        this.cbDataBase.Items.Add(reader["name"].ToString());
                    }
                    reader.Close();
                }
            }
            else
            {
                MessageBox.Show("连接失败！", "提示",  MessageBoxButtons.OK,MessageBoxIcon.Question);
            }
        }

        /// <summary>
        /// 根据设置得到连接字符串;
        /// </summary>
        /// <returns></returns>
        private string GetConnectionString()
        {
            string connectionstring = string.Empty;
            if (this.cbAuthentication.SelectedIndex == 0)
            {
                //sql身份验证
                connectionstring = string.Format("server={0};database={1};uid={2};pwd={3}", cbServer.Text, cbDataBase.Text, txbUserName.Text, txbPwd.Text);
            }
            else
            {
                connectionstring = string.Format("server={0};database={1};Integrated Security=true;", cbServer.Text, cbDataBase.Text);
            }
            return connectionstring;
        }

        //确定按钮
        private void btnOK_Click(object sender, EventArgs e)
        {
            ConnectionModel model = new ConnectionModel();
            model.ConnectionString = GetConnectionString();
            model.DataBaseName = cbDataBase.Text;
            model.Provider = "System.Data.SqlClient";
            
            Connections conn = new Connections();
            if (conn.Add(model))
            {
                dbTree.LoadDataBase();
                this.Close();
            }
            else
            {
                MessageBox.Show("保存失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
        }      
    }
}
